package com.hnas.common.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hnas.common.service.VerifyCodeService;
import com.hnas.common.service.dto.DTOVerifyCode;
import com.hnas.common.service.dto.search.DTOVerifyCodeSearch;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.dto.DataBean;

@RequestMapping("/backend/verifyCode")
@Controller
public class VerifyCodeController {

	private static Log log = LogFactory.getLog(VerifyCodeController.class);
	
	@Autowired
	private VerifyCodeService verifyCodeService;
	
	@RequestMapping("search")
	public String search(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("criteria")DTOVerifyCodeSearch criteria){
		criteria.setTotalCount(-1);
		DTOPagedList<DTOVerifyCode> pg = this.verifyCodeService.search(criteria);
		modelMap.put("pg", pg);
		modelMap.put("dto", criteria);
		
		List<DataBean> beans = new ArrayList<DataBean>();//控制页面表格显示的列
		beans.add(new DataBean("phoneNo","phoneNo","手机号",false,150));
		beans.add(new DataBean("verifyCode","verifyCode","验证码",false,150));
		beans.add(new DataBean("verifyType","verifyType","验证码类型",false,150));
		beans.add(new DataBean("createTime","createTime","创建时间",false,150));
		beans.add(new DataBean("expiryEndTime","expiryEndTime","过期时间",false,150));
		beans.add(new DataBean("effectTime","effectTime","有效时间数",false,150));
		beans.add(new DataBean("status","status","验证码状态",false,150));
		beans.add(new DataBean("appId","appId","应用标识",false,150));
		modelMap.put("beans", beans);
		
		return "/backend/account/list_verify_code";
	}
	
	/*@RequestMapping("/obtain")
	@ResponseBody
	public BaseAjaxResponse getVerifyCode(HttpServletRequest request,HttpResponse response){
		BaseAjaxResponse res = new BaseAjaxResponse();
		String phoneNo = request.getParameter("phoneNo");
		String verifyType = request.getParameter("verifyType");
		//TODO 获取验证码，然后存库，返回给调用方
		JSONObject jo = new JSONObject();
		jo.put(CommonConstants.Tel, phoneNo);
		jo.put(CommonConstants.Type, verifyType);
		try {
			DTOApiResponse dtoApiResponse = this.postConnServer.getRequestData(jo, RouteFinalValue.APP_UserVerification);
			String resultCode = null;
			String resultData = null;
			String resultMessage = null;
			if (null != dtoApiResponse) {
				DTOResult dtoResult = dtoApiResponse.getResult();
				resultCode = dtoResult.getResultCode();
				resultData = dtoResult.getResultData();
				resultMessage = dtoResult.getResultMsg();
			}
			if(null != resultCode){
				if (ResultCode.SUCCESS.equals(resultCode)) {
					log.info(DateUtil.dateTimeToString3(new Date()) + "获取短信验证码成功");
					AESTool aes = new AESTool();
					resultData = aes.decrypt(resultData, dtoApiResponse.getKey());// 解密
					System.out.println("请求返回resultData解密:" + resultData);
					if (resultData != null || !"nan".equals(resultData.toLowerCase())) {
						JSONObject result = JSON.parseObject(resultData);
						//保存验证码
						DTOVerifyCode dtoVerifyCode = new DTOVerifyCode();
						dtoVerifyCode.setId(CommonUtils.getUUID());
						dtoVerifyCode.setVerifyCode(result.getString(CommonConstants.verification));
						dtoVerifyCode.setPhoneNo(phoneNo);
						dtoVerifyCode.setVerifyType(Integer.valueOf(verifyType));
						dtoVerifyCode.setCreateTime(DateUtil.dateTimeToString3(new Date()));
						dtoVerifyCode.setUpdateTime(DateUtil.dateTimeToString3(new Date()));
						VerifyCode verifyCode = verifyCodeService.saveOrUpdate(dtoVerifyCode);
						if(verifyCode != null){
							//返回用户正确的验证码
							res.setResult(BaseAjaxResponse.SUCCESS);
							JSONObject joRes = new JSONObject();
							joRes.put(CommonConstants.verifyType, result.getString(CommonConstants.verification));
							res.setResultData(joRes);
							res.setErrorMessage("获取短息验证码成功");
						}else{
							res.setResult(BaseAjaxResponse.FAIL);
							res.setErrorMessage("获取短信验证码失败");
							System.out.println("获取短信验证码失败,验证码保存失败，"+resultMessage);
							log.info(DateUtil.dateTimeToString3(new Date()) + "获取短信验证码失败,验证码保存失败，"+resultMessage);
						}
					}else{
						res.setResult(BaseAjaxResponse.FAIL);
						res.setErrorMessage("获取短信验证码失败,"+resultMessage);
						System.out.println("获取短信验证码失败,"+resultMessage);
						log.info(DateUtil.dateTimeToString3(new Date()) + "获取短信验证码失败,"+resultMessage);
					}
					
				}else{
					res.setResult(BaseAjaxResponse.FAIL);
					res.setErrorMessage("获取短信验证码失败,"+resultMessage);
					System.out.println("获取短信验证码失败,"+resultMessage);
					log.info(DateUtil.dateTimeToString3(new Date()) + "获取短信验证码失败,"+resultMessage);
				}
			} else {
				res.setResult(BaseAjaxResponse.FAIL);
				res.setErrorMessage("获取短信验证码失败，服务异常");
				System.out.println("获取短信验证码失败,服务异常");
				log.info(DateUtil.dateTimeToString3(new Date()) + "获取短信验证码失败,服务异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.setResult(BaseAjaxResponse.FAIL);
			res.setErrorMessage("获取短信验证码失败");
			System.out.println("获取短信验证码失败"+e.getMessage());
			log.info(DateUtil.dateTimeToString3(new Date()) + "获取短信验证码失败,"+e.getMessage());
		}
		return res;
	}*/
}
